# -*- coding: utf-8 -*-
'''
Created on Mar 9, 2013

@author: LONG HOANG GIANG
'''
import sys, os
import workerpool
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import pyLib
import re, json
from urlparse import urljoin
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')


class MangaFc():
    
    url = ''
    
    def __init__(self, url):
        self.url = url
    
    def getListStory(self):
        tree = pyLib.loadWeb(self.url).build_tree()
        data = []
        for node in tree.xpath("//div[@class='manga-top']//h2/a"):
            link = node.get('href')
            if link == '': continue
            link = urljoin(self.url, link)
            print link
            data.append(link)
        return data
    
    def getListChapter(self, url):
        tree = pyLib.loadWeb(url).build_tree()
        data = []
        for node in tree.xpath("//tr[@class='regular']/td[1]//a"):
            name = pyLib.stringify(node)
            link = node.get('href')
            if link == '': continue
            link = urljoin(url, link)
            print name, link
            data.append({'name': name, 'url': link, 'chapid': pyLib.crc32unsigned(link)})
        data.reverse()
        return data
    
    def getImagesOfChapter(self, chapter):
        logging.info("chapter: {0}".format(chapter['name']))
        url = chapter['url']
        if not url.endswith("/"): url += "/"
        url += "nhieutrang.html"
        tree = pyLib.loadWeb(url).build_tree()
        data = []
        for node in tree.xpath("//div[@class='nhieutrang']/a/img"):
            imageUrl = node.get('src')
            if imageUrl == '': continue
            imageUrl = urljoin(url, imageUrl)
            imageUrl = re.sub(r"\?imgmax=\d+", "?imgmax=900", imageUrl)
            print imageUrl
            data.append(imageUrl)
    
        if self.output_files == None: raise
        pyLib.gz_file_put_content(self.output_files + "/" + chapter['chapid'], pyLib.encryptCipher(json.dumps(data)))
    
    def process(self):
        
        self.output = "/longhoanggiang/comic/do-re-mon"
        self.output_files = self.output + "/files"
        pyLib.createIfNotExistsPath(self.output_files)
         
        stories = self.getListStory()
        data = []
        for s in stories:
            chapters = self.getListChapter(s)
            for c in chapters:
                data.append(c)
        
        pool = workerpool.WorkerPool(size=4)
        pool.map(self.getImagesOfChapter, data)
        pool.shutdown()
        pool.wait()
        
        for d in data:
            del d['url']
        pyLib.gz_file_put_content(self.output + "/data", pyLib.encryptCipher(json.dumps(data)))
        
        

if __name__ == '__main__':
    
    logging.info("start get doremon")
    m = MangaFc('http://mangafc.com/truyen-tranh-doremon.htm')
#    m.getListStory()
#    m.getListChapter('http://mangafc.com/truyen-tranh/doraemon-truyen-dai-1167/')
#    m.getImagesOfChapter('http://mangafc.com/truyen-tranh/doraemon-truyen-dai-1167/cuoc-phieu-luu-vao-long-dat-1-22421/')
    m.process()
    
    logging.info("finished")
    os._exit(1)
    
    